<?php

namespace Es\Struct;

use Es\Constant\IndexConst;
use Es\Contract\FieldsInterface;
use Es\Struct\Fields\DynamicStruct;

/**
 * 索引结构体
 */
class IndexMappingsStruct 
{

    private $dynamic      = [];
    private $fields    = [];

    private $mappings     = [
        'dynamic'    => IndexConst::DYNAMIC_TRUE,
        'properties' => []
    ];

    public function setDynamic(DynamicStruct $dynamicStruct): IndexMappingsStruct
    {
        $this->dynamic = $dynamicStruct->asArray();
        return $this;
    }
    public function setFields(FieldsInterface $FieldsStruct): IndexMappingsStruct
    {
        [$name, $content] = $FieldsStruct->asArray();
        $this->fields[$name] = $content;
        return $this;
    }

    public function asArray(): array
    {
        if(!empty($this->dynamic)){
            $this->mappings = $this->dynamic;
        }
        if(!empty($this->fields)){
            $this->mappings['properties'] = $this->fields;
        }
        return $this->mappings;
    }
}
